package cn.edu.ujn.bmis.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.dispatcher.FilterDispatcher;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.model.Book;
import cn.edu.ujn.bmis.model.BrRtRn;
import cn.edu.ujn.bmis.model.OrderBook;
import cn.edu.ujn.bmis.model.SysPara;
import cn.edu.ujn.bmis.model.UserInfo;
import cn.edu.ujn.bmis.service.BookManager;
import cn.edu.ujn.bmis.service.BrRtRnManager;
import cn.edu.ujn.bmis.service.OrderBookManager;
import cn.edu.ujn.bmis.service.SysParaManager;
import cn.edu.ujn.bmis.service.UserInfoManager;
import cn.edu.ujn.bmis.tools.Constant;
import cn.edu.ujn.bmis.tools.Helper;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("BrRtRn")
@Scope("prototype")
public class BrRtRnAction extends ActionSupport {

	private static final long serialVersionUID = -5759958247945329089L;
	private BrRtRn brRtRn;
	private BrRtRnManager brRtRnManager;
	private String targetType = "navTab"; // navTab或dialog，用来标记是navTab上的分页还是dialog上的分页
	private List<BrRtRn> brRtRnList = new ArrayList<BrRtRn>();
	private Helper helper;
	private int pageNum = 1;
	private int currentPage = 1; // 当前是第几页
	private int totalCount = 0; // 总条数
	private int numPerPage = 5; // 每页显示多少条
	private int pageNumShown = 5; // 页标数字多少个
	private int pageCount = 0; // 总页数
	private String orderField;
	private String orderDirection;
	private long bookId;
	private String bookName;
	private BookManager bookManager;
	private List<Book> bookList = new ArrayList<Book>();
	private String borrowTime;
	private String renewTime;
	private String returnTime;
	private String bOperatorId;
	private String userId;// 借阅者编号
	private String rnOperatorId;//续借操作员工号
	private String rnOperatorName;//续借操作员姓名
	private String rtOperatorId;//归还操作员工号
	private String rtOperatorName;//归还操作员姓名
	private long id;
	private String startTime;
	private String endTime;
	private OrderBookManager orderBookManager;
	private List<OrderBook> orderBookList = new ArrayList<OrderBook>();
	private String returnOK;
	private String returnNo;
	private String readerId;
	private String functionNum;
	private UserInfoManager userInfoManager;
	private SysParaManager sysParaManager;
	private List<SysPara> sysParaList = new ArrayList<SysPara>();
	private String bookType;
	private String endBorrowTime;
	
	
	/**
	 * 查看统计图书超期记录信息
	 */
	@SuppressWarnings("unchecked")
	public String findOverdueBrRtRn(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql_type="";
		String hql="";
		String hql_size="";
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String today = formatter.format(date);
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		hql=" select brr from BrRtRn brr,UserInfo u,Book b where brr.brUser.userId=u.userId and brr.book.bookId=b.bookId and brr.returnTime=null " +
				" and ((brr.renewTime=null and brr.endBorrowTime<CONVERT(datetime,'"+today+"')) or (brr.renewTime!=null and brr.endRenewTime<CONVERT(datetime,'"+today+"'))) ";
		hql_size=" select count(*) from BrRtRn brr,UserInfo u,Book b where brr.brUser.userId=u.userId and brr.book.bookId=b.bookId and brr.returnTime=null " +
		" and ((brr.renewTime=null and brr.endBorrowTime<CONVERT(datetime,'"+today+"')) or (brr.renewTime!=null and brr.endRenewTime<CONVERT(datetime,'"+today+"'))) ";
		if(readerId!=null&&!"".equals(readerId)){
			hql += " and brr.brUser.userId='"+readerId.trim()+"' ";
			hql_size += " and brr.brUser.userId='"+readerId.trim()+"' ";
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql+=" and brr.book.bookType='"+bookType.trim()+"' ";
			hql_size+=" and brr.book.bookType='"+bookType.trim()+"' ";
		}
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			hql+=" and brr.borrowTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"') ";
			hql_size+=" and brr.borrowTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"') ";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		brRtRnList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}
	
	/**
	 * 进入图书管理员查看、统计图书借阅信息页面
	 */
	public String beforeFindBorrowBrRtRn(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql_type="";
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		return SUCCESS;
	}
	
	/**
	 * 图书管理员查看、统计图书借阅信息
	 */
	@SuppressWarnings("unchecked")
	public String findBorrowBrRtRn(){
		
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql="";
		String hql_size="";
		String hql_type="";
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		this.currentPage = this.pageNum;
		hql="select brr from BrRtRn brr where brr.borrowTime!=null";
		hql_size="select count(*) from BrRtRn brr where brr.borrowTime!=null";
		if ("OK".equals(returnOK)&&!"NO".equals(returnNo)) {
			hql += " and brr.returnTime!=null";
			hql_size += " and brr.returnTime!=null";
		} else if (!"OK".equals(returnOK)&&"NO".equals(returnNo)) {
			hql += " and brr.returnTime=null";
			hql_size += " and brr.returnTime=null";
		}
		if(readerId!=null&&!"".equals(readerId)){
			hql += " and brr.brUser.userId='"+readerId.trim()+"'";
			hql_size += " and brr.brUser.userId='"+readerId.trim()+"'";
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql+=" and brr.book.bookType='"+bookType.trim()+"' ";
			hql_size+=" and brr.book.bookType='"+bookType.trim()+"' ";
		}
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			hql+=" and brr.borrowTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"')";
			hql_size+=" and brr.borrowTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"')";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		brRtRnList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}
	
	/**
	 * 进入读者查看自己的借阅记录页面
	 */
	public String beforeReaderFindBrRtRn(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql="";
		hql="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql);
		return SUCCESS;
		
	}
	
	/**
	 * 读者查看自己的借阅记录
	 */
	@SuppressWarnings("unchecked")
	public String readerFindBrRtRn(){
		String hql="";
		String hql_size="";
		String hql_type="";
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		this.currentPage = this.pageNum;
		hql="select brr from Book b,Donator d,BrRtRn brr where b.donator.donatorId=d.donatorId " +
				"and brr.book.bookId=b.bookId and brr.brUser.userId='"+users.getUserId()+"'";
		hql_size="select count(*) from Book b,Donator d,BrRtRn brr where b.donator.donatorId=d.donatorId " +
		"and brr.book.bookId=b.bookId and brr.brUser.userId='"+users.getUserId()+"'";
		if ("OK".equals(returnOK)&&!"NO".equals(returnNo)) {
			hql += " and brr.returnTime!=null";
			hql_size += " and brr.returnTime!=null";
		} else if (!"OK".equals(returnOK)&&"NO".equals(returnNo)) {
			hql += " and brr.returnTime=null";
			hql_size += " and brr.returnTime=null";
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql+=" and b.bookType='"+bookType.trim()+"' ";
			hql_size+=" and b.bookType='"+bookType.trim()+"' ";
		}
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			hql+=" and brr.borrowTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"')";
			hql_size+=" and brr.borrowTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"')";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		brRtRnList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}
	

	/**
	 * 查看可借的书籍
	 * 满足的条件
	 * 1、图书的是否可借标志为可借b.borrowOK=1
	 * 2、图书没有借出br.returnTime=null
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findBorrowBook() {
		String hql = "";
		String hql_size = "";
		String hql_type="";
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		this.currentPage = this.pageNum;
		hql = "select b from Book b,Donator d where b.donator.donatorId=d.donatorId " +
				"and b.borrowOK=1 and not exists(select 1 from BrRtRn br where b.bookId=br.book.bookId and br.returnTime=null)";
		hql_size = "select count(*) from Book b,Donator d where b.donator.donatorId=d.donatorId " +
				"and b.borrowOK=1 and not exists(select 1 from BrRtRn br where b.bookId=br.book.bookId and br.returnTime=null)";
		if (bookName != null && !"".equals(bookName)) {
			hql += " and b.bookName='" + bookName.trim() + "'";
			hql_size += " and b.bookName='" + bookName.trim() + "'";
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql += " and b.bookType='" + bookType.trim() + "'";
			hql_size += " and b.bookType='" + bookType.trim() + "'";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		bookList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}

	/**
	 * 进入借阅页面
	 */
	@SuppressWarnings("unchecked")
	public String beforeAddBrRtRn() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		bOperatorId = users.getUserId();
		Date today = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		borrowTime = dateFormat.format(today);
		return SUCCESS;
	}
	
	/**
	 * 进入续借页面
	 */
	@SuppressWarnings("unchecked")
	public String beforeAddRenew(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql="";
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		rnOperatorId = users.getUserId();
		rnOperatorName = users.getUserName();
		Date today = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		renewTime = dateFormat.format(today);
		hql="select brr from BrRtRn brr where brr.id="+id+"";
		brRtRnList=helper.find(hql);
		if(brRtRnList.size()>0){
			brRtRn=brRtRnList.get(0);
		}
		return SUCCESS;
	}
	
	/**
	 * 进入归还页面
	 */
	@SuppressWarnings("unchecked")
	public String beforeAddReturn(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql="";
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		rtOperatorId = users.getUserId();
		rtOperatorName = users.getUserName();
		Date today = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		returnTime = dateFormat.format(today);
		hql="select brr from BrRtRn brr where brr.id="+id+"";
		brRtRnList=helper.find(hql);
		if(brRtRnList.size()>0){
			brRtRn=brRtRnList.get(0);
		}
		return SUCCESS;
	}

	/**
	 * 添加借阅记录
	 * 
	 * 1、需要判断这本书是否被预约了，如果是别人预约的不可以借了
	 * 如果是自己预约的需要更新更新预约表中记录的effective=0
	 * 2、判断这个读者是否借阅量达到最大值5本，如果到了则不允许再借阅
	 */
	public void addBrRtRn() {
		long bookId=0;
		String hql="";
		String hql2="";
		String hql_check="";
		String userIdTemp="";
		List<UserInfo> userInfoList=new ArrayList<UserInfo>();
		//检查输入的读者编号是否存在
		hql_check="from UserInfo u where u.userId='"+brRtRn.getBrUser().getUserId()+"'";
		userInfoList=userInfoManager.findUserInfo(hql_check);
		if(userInfoList.size()<=0){
			Helper.out("300", "输入的读者编号不存在请检查！", "", "", "", "");
			return;
		}
		brRtRn.setbOperatorId(bOperatorId);
		
		//需要判断这本书是否被预约了，如果是别人预约的不可以借了,如果是自己预约的需要更新更新预约表中记录的effective=0
		if(brRtRn!=null){
			bookId=brRtRn.getBook().getBookId();
			hql="from OrderBook o where o.book.bookId="+bookId+" and o.orderTime!=null and o.effective=1";
			orderBookList=orderBookManager.findOrderBook(hql);
			if(orderBookList.size()>0){
				userIdTemp=orderBookList.get(0).getUserInfo().getUserId();
				if(userIdTemp.equals(brRtRn.getBrUser().getUserId())){
					hql2 = "update OrderBook o set o.effective=0 where o.id='"
						+ orderBookList.get(0).getId() + "'";
					orderBookManager.updateOrderBook(hql2);
				}else{
					Helper.out("300", "图书借阅失败,图书已经被编号为"+orderBookList.get(0).getUserInfo().getUserId()+"的" +
							""+orderBookList.get(0).getUserInfo().getUserName()+ orderBookList.get(0).getOrderTime()+"预约！", "", "", "", "");
					return;
				}
			}
		}
		//判断这个读者是否借阅量达到最大值5本，如果到了则不允许再借阅
		hql="from BrRtRn brr where brr.brUser.userId='"+brRtRn.getBrUser().getUserId()+"' and brr.returnTime=null";
		brRtRnList=brRtRnManager.findBrRtRn(hql);
		if(brRtRnList.size()>=Constant.borrowBookCount){
			Helper.out("300", "图书借阅量已经达到最大值了，不能再借阅量！", "", "", "", "");
			return;
		}
		//借阅到期时间为30天
		Date today = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = new GregorianCalendar();
		cal.setTime(today);
		cal.add(Calendar.DATE,Constant.borrowTime);
		Date d = cal.getTime();
		String endBorrowTime  = dateFormat.format(d);
		brRtRn.setEndBorrowTime(endBorrowTime);
		if (brRtRnManager.save(brRtRn)) {
			Helper.out("200", "图书借阅成功", "closeCurrent", "", "", "borrowBook");
		} else {
			Helper.out("300", "图书借阅失败", "", "", "", "");
		}
	}
	
	/**
	 * 添加续借记录
	 */
	@SuppressWarnings("unchecked")
	public void addRenewBrRtRn(){
		String hql="";
		String endRenewTime="";
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		//续借到期时间为30天
		try {
			Date end ;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			end=dateFormat.parse(endBorrowTime);
			Calendar cal = new GregorianCalendar();
			cal.setTime(end);
			cal.add(Calendar.DATE,Constant.borrowTime);
			Date d = cal.getTime();
			endRenewTime  = dateFormat.format(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		hql="update BrRtRn brr set brr.renewTime='"+renewTime+"',brr.endRenewTime='"+endRenewTime+"',brr.rnOperatorId='"+rnOperatorId+"' where brr.id='"+id+"'";
		if(brRtRnManager.update(hql)){
			if("读者".equals(users.getUserStatus())){
				Helper.out("200", "图书续借成功", "closeCurrent", "", "", "renewBook");
			}else{
				Helper.out("200", "图书续借成功", "closeCurrent", "", "", "findAdRenewBook");
			}
		}else{
			Helper.out("300", "图书续借失败", "", "", "", "");
		}
	}
	
	/**
	 * 添加归还记录
	 * 
	 * 归还时需要添加是否超期，如果超期不允许归还
	 * 给出提示信息
	 */
	public void addReturnBrRtRn(){
		String hql="";
		String hql_check="";
		String pay="";
		Date end;
		Date start;
		Date date=new Date();
		long daysBetween=0;
		SimpleDateFormat formatter=new SimpleDateFormat ("yyyy-MM-dd");
		String now = formatter.format(date);
		Date d1 = null;
		try
		{
		d1 = formatter.parse(now);
		}catch(Exception e){
		}
		Calendar cal = new GregorianCalendar();
		cal.setTime(d1);
		cal.add(Calendar.DATE,-30);
		Date d2 = cal.getTime();
		String datestr2 = formatter.format(d2);
		hql_check="from BrRtRn brr where brr.id="+id+" and brr.borrowTime<CONVERT(datetime,'"+datestr2+"') and brr.overdueDay=0";
		brRtRnList=brRtRnManager.findBrRtRn(hql_check);
		if(brRtRnList.size()>0){
			try {
				start=formatter.parse(brRtRnList.get(0).getBorrowTime());
				end=formatter.parse(now);
				daysBetween=(end.getTime()-start.getTime()+1000000)/(3600*24*1000);
				pay=String.valueOf(daysBetween*Constant.money);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			Helper.out("300", "图书超期，"+daysBetween+"天，罚款"+pay+"元,请先进行超期处理，然后再归还", "", "", "", "");
			return;
		}
		
		hql="update BrRtRn brr set brr.returnTime='"+returnTime+"',brr.rtOperatorId='"+rtOperatorId+"' where brr.id="+id+"";
		if(brRtRnManager.update(hql)){
			Helper.out("200", "图书归还成功", "closeCurrent", "", "", "returnBook");
		}else{
			Helper.out("300", "图书归还失败", "", "", "", "");
		}
	}
	
	/**
	 * 图书超期处理
	 * 
	 * brr.overdueDay=1 表示超期处理成功
	 * brr.overdueDay=0，表示尚未超期处理
	 */
	public void overDayBrRtRn(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql="";
		String hql_check="";
		String hql_find="";
		Date start;
		Date end;
		long daysBetween=0;
		String pay="";
		
		Date date=new Date();
		SimpleDateFormat formatter=new SimpleDateFormat ("yyyy-MM-dd");
		String now = formatter.format(date);
		Date d1 = null;
		try
		{
		d1 = formatter.parse(now);
		}catch(Exception e){
		}
		Calendar cal = new GregorianCalendar();
		cal.setTime(d1);
		cal.add(Calendar.DATE,-30);
		Date d2 = cal.getTime();
		String datestr2 = formatter.format(d2);
		hql_check="from BrRtRn brr where brr.id="+id+" and brr.borrowTime>CONVERT(datetime,'"+datestr2+"')";
		brRtRnList=brRtRnManager.findBrRtRn(hql_check);
		if(brRtRnList.size()>0){
			Helper.out("300", "图书没有超期，不用进行超期处理!", "", "", "", "");
			return;
		}
		
		hql_find=" from BrRtRn brr where brr.id="+id+" ";
		brRtRnList=brRtRnManager.findBrRtRn(hql_find);
		if(brRtRnList.size()>0){
			try {
				start=formatter.parse(brRtRnList.get(0).getBorrowTime());
				end=formatter.parse(now);
				daysBetween=(end.getTime()-start.getTime()+1000000)/(3600*24*1000);
				pay=String.valueOf(daysBetween*Constant.money);
				pay="超期"+daysBetween+"天，罚款"+pay+"元";
				hql="update BrRtRn brr set brr.overdueDay=1,brr.pay='"+pay+"' where brr.id="+id+"";
				if(brRtRnManager.update(hql)){
					Helper.out("200", "超期处理成功,可以进行图书归还", "", "", "", "");
				}else{
					Helper.out("300", "超期处理成功!", "", "", "", "");
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
	}

	/**
	 * 查看可以续借的图书
	 * 
	 * 续借的条件：
	 * 1、这本书不能被预约
	 * 2、这本书不能超期
	 * 3、这本书不能使以前的借阅记录brr.returnTime=null
	 * 4、这本书是自己的借阅记录brr.brUser.userId=登陆的id，如果是读者端的话
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findRenewBook() {
		String hql = "";
		String hql_size = "";
		String hql_type = "";
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		
		Date date=new Date();
		SimpleDateFormat formatter=new SimpleDateFormat ("yyyy-MM-dd");
		String now = formatter.format(date);
		Date d1 = null;
		try
		{
		d1 = formatter.parse(now);
		}catch(Exception e){
		}
		Calendar cal = new GregorianCalendar();
		cal.setTime(d1);
		cal.add(Calendar.DATE,-30);
		Date d2 = cal.getTime();
		String datestr2 = formatter.format(d2);
		this.currentPage = this.pageNum;
		hql = " select brr from BrRtRn brr where brr.borrowTime!=null and brr.renewTime=null and brr.returnTime=null " +
//				" and brr.brUser.userId='"+users.getUserId()+"' " +
				" and not exists(select 1 from OrderBook o where brr.book.bookId=o.book.bookId and o.orderTime!=null and effective=1) " +
				" and brr.borrowTime>CONVERT(datetime,'"+datestr2+"') ";
		hql_size = " select count(*) from BrRtRn brr where brr.borrowTime!=null and brr.renewTime=null and brr.returnTime=null " +
//				   "and brr.brUser.userId='"+users.getUserId()+"' " +
				   " and not exists(select 1 from OrderBook o where brr.book.bookId=o.book.bookId and o.orderTime!=null and effective=1) " +
				   " and brr.borrowTime>CONVERT(datetime,'"+datestr2+"') ";
		if (bookName != null && !"".equals(bookName)) {
			hql += " and brr.book.bookName like '%" + bookName.trim() + "%' ";
			hql_size += " and brr.book.bookName like '%" + bookName.trim() + "%' ";
		}
		//判断是读者端续借还是管理端续借
		if("读者".equals(users.getUserStatus())){
			hql += " and brr.brUser.userId='" + users.getUserId() + "' ";
			hql_size += " and brr.brUser.userId='" + users.getUserId() + "' ";
		}else{
			if (userId != null && !"".equals(userId)) {
				hql += " and brr.brUser.userId='" + userId.trim() + "' ";
				hql_size += " and brr.brUser.userId='" + userId.trim() + "' ";
			}
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql += " and brr.book.bookType='" + bookType.trim() + "'";
			hql_size += " and brr.book.bookType='" + bookType.trim() + "'";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		brRtRnList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}
	
	/**
	 * 查看可以取消续借的图书
	 * 1、以前归还的图书不能再取消续借brr.returnTime=null
	 */
	@SuppressWarnings("unchecked")
	public String findDelRenewBook() {
		String hql = "";
		String hql_size = "";
		String hql_type;
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		
		this.currentPage = this.pageNum;
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		hql = "select brr from BrRtRn brr where brr.renewTime !=null and brr.returnTime=null ";
		hql_size = "select count(*) from BrRtRn brr where brr.renewTime !=null and brr.returnTime=null ";
		if (bookName != null && !"".equals(bookName)) {
			hql += " and brr.book.bookName like '%" + bookName.trim() + "%'";
			hql_size += " and brr.book.bookName like '%" + bookName.trim() + "%'";
		}
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			hql+=" and brr.renewTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"')";
			hql_size+=" and brr.renewTime between CONVERT(datetime, '"+startTime.trim()+"') and CONVERT(datetime, '"+endTime.trim()+"')";
		}
		//判断是读者端续借还是管理端续借
		if("读者".equals(users.getUserStatus())){
			hql += " and brr.brUser.userId='" + users.getUserId() + "' ";
			hql_size += " and brr.brUser.userId='" + users.getUserId() + "' ";
		}else{
			if (userId != null && !"".equals(userId)) {
				hql += " and brr.brUser.userId='" + userId.trim() + "' ";
				hql_size += " and brr.brUser.userId='" + userId.trim() + "' ";
			}
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql += " and brr.book.bookType='" + bookType.trim() + "'";
			hql_size += " and brr.book.bookType='" + bookType.trim() + "'";
		}
		
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		brRtRnList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}
	
	/**
	 * 取消续借操作
	 */
	@SuppressWarnings("unchecked")
	public void delRenewBookBrRtRn(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
			return;
		}
		String hql="";
		Map session = (Map) ActionContext.getContext().getSession();
		UserInfo users = new UserInfo();
		users = (UserInfo) session.get("user");
		hql="update BrRtRn brr set brr.renewTime=null,brr.rnOperatorId=null where brr.id='"+id+"'";
		if(brRtRnManager.update(hql)){
			if("读者".equals(users.getUserStatus())){
				Helper.out("200", "图书解续成功", "", "", "delRenewBook", "");
			}else{
				Helper.out("200", "图书解续成功", "", "", "findAdDelRenewBook", "");
			}
		}else{
			Helper.out("300", "图书解续失败", "", "", "", "");
		}
	}
	
	/**
	 * 查看要归还处理的图书
	 */
	@SuppressWarnings("unchecked")
	public String findReturnBook() {
		String hql = "";
		String hql_size = "";
		String hql_type="";
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		hql_type="select s.value from SysPara s where s.name='图书种类'";
		sysParaList=sysParaManager.findSysPara(hql_type);
		hql = "select brr from BrRtRn brr where brr.borrowTime!=null and brr.returnTime=null";
		hql_size = "select count(*) from BrRtRn brr where brr.borrowTime!=null and brr.returnTime=null";
		if (bookName != null && !"".equals(bookName)) {
			hql += " and brr.book.bookName='" + bookName.trim() + "'";
			hql_size += " and brr.book.bookName='" + bookName.trim() + "'";
		}
		if (userId != null && !"".equals(userId)) {
			hql += " and brr.brUser.userId='" + userId.trim() + "'";
			hql_size += " and brr.brUser.userId='" + userId.trim() + "'";
		}
		if(bookType!=null&&!"".equals(bookType)){
			hql += " and brr.book.bookType='" + bookType.trim() + "'";
			hql_size += " and brr.book.bookType='" + bookType.trim() + "'";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		brRtRnList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}

	public BrRtRn getBrRtRn() {
		return brRtRn;
	}

	public void setBrRtRn(BrRtRn brRtRn) {
		this.brRtRn = brRtRn;
	}

	public BrRtRnManager getBrRtRnManager() {
		return brRtRnManager;
	}

	@Resource(name = "brRtRnManager")
	public void setBrRtRnManager(BrRtRnManager brRtRnManager) {
		this.brRtRnManager = brRtRnManager;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public List<BrRtRn> getBrRtRnList() {
		return brRtRnList;
	}

	public void setBrRtRnList(List<BrRtRn> brRtRnList) {
		this.brRtRnList = brRtRnList;
	}

	public Helper getHelper() {
		return helper;
	}

	@Resource(name = "helper")
	public void setHelper(Helper helper) {
		this.helper = helper;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public long getBookId() {
		return bookId;
	}

	public void setBookId(long bookId) {
		this.bookId = bookId;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public BookManager getBookManager() {
		return bookManager;
	}

	@Resource(name = "bookManager")
	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}

	public List<Book> getBookList() {
		return bookList;
	}

	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}

	public String getBorrowTime() {
		return borrowTime;
	}

	public void setBorrowTime(String borrowTime) {
		this.borrowTime = borrowTime;
	}

	public String getBOperatorId() {
		return bOperatorId;
	}

	public void setBOperatorId(String bOperatorId) {
		this.bOperatorId = bOperatorId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getRenewTime() {
		return renewTime;
	}

	public void setRenewTime(String renewTime) {
		this.renewTime = renewTime;
	}

	public String getRnOperatorId() {
		return rnOperatorId;
	}

	public void setRnOperatorId(String rnOperatorId) {
		this.rnOperatorId = rnOperatorId;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getRnOperatorName() {
		return rnOperatorName;
	}

	public void setRnOperatorName(String rnOperatorName) {
		this.rnOperatorName = rnOperatorName;
	}

	public String getRtOperatorId() {
		return rtOperatorId;
	}

	public void setRtOperatorId(String rtOperatorId) {
		this.rtOperatorId = rtOperatorId;
	}

	public String getRtOperatorName() {
		return rtOperatorName;
	}

	public void setRtOperatorName(String rtOperatorName) {
		this.rtOperatorName = rtOperatorName;
	}

	public String getReturnTime() {
		return returnTime;
	}

	public void setReturnTime(String returnTime) {
		this.returnTime = returnTime;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public OrderBookManager getOrderBookManager() {
		return orderBookManager;
	}
	
	@Resource(name="orderBookManager")
	public void setOrderBookManager(OrderBookManager orderBookManager) {
		this.orderBookManager = orderBookManager;
	}


	public String getReturnOK() {
		return returnOK;
	}


	public void setReturnOK(String returnOK) {
		this.returnOK = returnOK;
	}


	public String getReturnNo() {
		return returnNo;
	}


	public void setReturnNo(String returnNo) {
		this.returnNo = returnNo;
	}

	public String getReaderId() {
		return readerId;
	}

	public void setReaderId(String readerId) {
		this.readerId = readerId;
	}

	public String getFunctionNum() {
		return functionNum;
	}

	public void setFunctionNum(String functionNum) {
		this.functionNum = functionNum;
	}

	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}

	@Resource(name="userInfoManager")
	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}

	public SysParaManager getSysParaManager() {
		return sysParaManager;
	}

	@Resource(name="sysParaManager")
	public void setSysParaManager(SysParaManager sysParaManager) {
		this.sysParaManager = sysParaManager;
	}

	public List<SysPara> getSysParaList() {
		return sysParaList;
	}

	public String getBookType() {
		return bookType;
	}

	public void setBookType(String bookType) {
		this.bookType = bookType;
	}

	public String getEndBorrowTime() {
		return endBorrowTime;
	}

	public void setEndBorrowTime(String endBorrowTime) {
		this.endBorrowTime = endBorrowTime;
	}
	
}
